<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-11-13
 * Time: 上午9:35
 */

namespace App\Http\Controllers\Admin\Basic;


use App\Http\Controllers\Admin\AdminController;
use App\Repositories;
use Illuminate\Http\Request;

class AdvDataController extends AdminController
{
    /**
     * 初始化操作
     * UserController constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->setTemplatePrefix(config('admin.systems.template_prefix', 'admin.').'basic.adv.data.');
    }

    /**
     * 获取当前控制器服务仓库对象
     * @return mixed
     */
    public function getServiceRepository() : Repositories\Basic\AdvRepository
    {
        return Repositories\Basic\AdvRepository::getInstance();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $advId = $request->route('adv_id');

        $advDetail = $this->getServiceRepository()->getDetailById($advId);

        $data = $this->getServiceRepository()->getAdvDataPaginateData($advId);

        return $this->fetch('index', ['data'=>$data, 'advDetail'=>$advDetail]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        $advId = $request->route('adv_id');

        $advDetail = $this->getServiceRepository()->getDetailById($advId);

        if (strtolower($request->method()) !== 'post') {

            $id = $request->route('id', 0);

            $detail = $this->getServiceRepository()->getAdvDataDetailById($id);
            $action = 'add';
            if (!empty($id)) {
                $action = 'edit';
            }
            return $this->fetch('edit', [
                'metaTitle' => '更新广告位数据信息',
                'postUrl'   => get_admin_url('basic/adv/data/edit', ['adv_id'=>$advId]),
                'detail'    => $detail,
                'action'    => $action,
                'advDetail' => $advDetail,
            ]);
        }

        $info = $request->input('info', []);
        $cover = get_value($info, 'cover', []);
        $info['adv_id'] = $advId;
        $info['cover'] = get_value($cover, 'url', '');
        $info['cover_id'] = get_value($cover, 'id', 0);

        if ($advDetail->type == 2 || $advDetail->type == 3) {
            $content = get_value($info, 'content', []);
            $info['content'] = get_value($content, 'url', '');
        }

        $action = $request->input('action', 'add');

        $this->getServiceRepository()->saveData($info, $action);

        return $this->success('广告信息保存成功');
    }

    /**
     * 删除数据
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function delete(Request $request)
    {
        if (strtolower($request->method()) == 'get') {
            $ids = $request->route('ids', '');
        } else {
            $ids = $request->input('ids', []);
        }

        if (!is_array($ids)) {
            $ids = explode(',', $ids);
        }

        $re = $this->getServiceRepository()->deleteDataByIds($ids);

        if (!$re) {
            return $this->error("删除失败");
        }
        return $this->success("删除成功");
    }
}